<template>
  <div class="page_content">
    <div v-if="isPageLoading" class="page_loading fixed">
      <van-loading size="24px" vertical color="#fff">加载中...</van-loading>
    </div>
    <header>
      <van-nav-bar class="main_header" title="个人信息">
        <template #left>
          <span @click="goBack">返回</span>
        </template>
        <template #right>
          <span @click="submitRefresh">确定</span>
        </template>
      </van-nav-bar>
    </header>
    <div class="main_content">
      <editIndividual ref="editIndividual" :user-data="individualInfo" :is-default-check="!!individualInfo.isApproved" />
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex';
import EditIndividual from '@/viewMobile/components/PersonalInfo';
import {
  api_GET_EventsManagementRegisterId,
  api_PUT_EventsManagementRegisterId,
  api_POST_EventsManagementRegister
} from '@/extend/api/gameswaggerApi/app';
export default {
  components: {
    EditIndividual
  },
  data() {
    return {
      isPageLoading: false,
      individualInfo: {},
      createRegisterInput: null
    };
  },
  computed: {
    ...mapState({
      currentUserType: state => state.user.currentUserType
    })
  },
  watch: {
    currentUserType: {
      handler(val) {
        if (!val) return;
        this.getIndividualInfo();
      }
    }
  },
  created() {
    this.gameId = this.$route.params.id;
    if (this.currentUserType) {
      this.getIndividualInfo();
    }
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    // 获取个人数据
    getIndividualInfo() {
      if (!this.currentUserType.registerId) return;
      api_GET_EventsManagementRegisterId(
        this.currentUserType.registerId,
        { IncludeGender: true, IncludeIdCardType: true }
      ).then(res => {
        this.individualInfo = res;
        this.$store.dispatch('user/getSaveCurrentUserIndInfo', res);
      }, error => {
        this.$toast.fail(`获取个人信息失败！${error.error.message}`);
      });
    },
    goBack() {
      this.$router.push({
        path: `/myCenter${this.gameId && '/' + this.gameId || ''}/individual`
      });
    },
    submitRefresh() {
      this.$refs.editIndividual.getInsideUserData().then(res => {
        if (res.imagePath) {
          this.createRegisterInput = res;
          this.createRegisterInput.isCurrentUser = true;
          this.createRegisterInput.joinCurrentDelegation = false;
          this.createRegisterInput.registerTypeCode = this.createRegisterInput.registerTypeCode || 'I';
          this.isPageLoading = true;
          if (this.currentUserType.registerId) {
            this.updateMemberInfo();
          } else {
            this.addMemberInfo();
          }
        } else {
          this.$toast({ type: 'fail', message: `请上传头像` });
        }
      });
    },
    // 新增个人信息
    addMemberInfo() {
      // 将空字符串转为Null
      for (const k in this.createRegisterInput) {
        const ktem = this.createRegisterInput[k];
        if (ktem === '') {
          this.createRegisterInput[k] = null;
        }
      }
      api_POST_EventsManagementRegister(this.createRegisterInput).then(() => {
        this.isPageLoading = false;
        this.dialogVisible = false;
        this.getIndividualInfo();
        this.$store.dispatch('user/getInfo');
        this.$store.dispatch('user/getCurrentUserType');
        this.$toast.success('操作成功');
      }, error => {
        this.$toast.fail(`${error.error.message}`);
        this.isPageLoading = false;
      });
    },
    // 更新个人信息
    updateMemberInfo() {
      api_PUT_EventsManagementRegisterId(this.currentUserType.registerId, this.createRegisterInput).then(() => {
        this.isPageLoading = false;
        this.dialogVisible = false;
        this.getIndividualInfo();
        this.$toast.success('操作成功');
      }, error => {
        this.$toast.fail(`${error.error.message}`);
        this.isPageLoading = false;
      });
    }
  }
};
</script>
